/**
 * 904. 水果成篮
 */
class Solution1 {
    public int totalFruit(int[] fruits) {
        int left = 0,  right = 0, n = fruits.length , count = 0, ret = Integer.MIN_VALUE;
        int[] hash = new int[n];

        while(right < n) {
            // 维护count
            if(hash[fruits[right]] == 0) count++;

            // 进窗口
            hash[fruits[right]]++;

            if(count <= 2) {
                // 需要更新
                ret = Math.max(ret, right - left + 1);
            } else {
                // 出窗口 (count >= 2)
                hash[fruits[left]]--;
                if(hash[fruits[left]] == 0) count--;
                left++;
            }

            right++;
        }

        return ret;
    }
}